//引入集合模型
const { usersModel } = require("../models/usersModel");
const { uploadFiles } = require("../util/handleFiles");
const jwt = require("jsonwebtoken");
async function login(req, res, next) {

    let {passWord,userPhone }=req.body;
    let students = await usersModel.find({passWord,userPhone});
    console.log(students);
    if (students.length > 0) {
         // 生成 token  ,
        const token = jwt.sign(
            { user: students[0] }, //  用户信息
            'user',  // 密钥，任意字符串
            { expiresIn: 1000 }//  设置有效期,纯数字以秒为单位， 支持字符串 "10 days"  "8h" 
        );
        //返回到前端并保存到本地
        res.send({
            message: "登陆成功",
            code: 200,
            token:"Bearer " + token,
            user: students[0]._id
        });
    }else{
        res.send({message:'登录失败',code:-1})
    }
   
}
//资料
async function data(req, res, next){
    let {user_id}=req.body
    let user = await usersModel.find({_id:user_id});
    res.send({
        user
    })
 }

//修改
async function submit (req, res, next) {
    // let {user_id,imagePath,userName,userGender}=req.body;
    let result = await usersModel.updateMany({_id:req.body.user_id},req.body);
    if (result.modifiedCount > 0) {
      res.send({ message: "修改成功", code:200})
    } else {
      res.send({ message: "修改失败", code:-1})
    }
}

module.exports = {
    login, data,submit
}